<?php
/**
 * Created by PhpStorm.
 * User: haobin
 * Date: 2016/9/22
 * Time: 12:17
 */
require_once('conn.php');
require_once('functions.php');
$sql = "select * from `link` where type = 2";
$row_user = $mysql->query($sql);
//验证
if ($_GET) {
    $uid = $_GET['id'];
    $openid = $_GET['jtime'];
    $userInfo = $mysql->query("select * from `user` WHERE `id` = {$uid}");
    if ($openid != $userInfo[0]['openid'] || $openid == null) {
        echo "<script>alert('非法操作！')</script>";
        echo "<script>location.href='./404.html'</script>";
    }
}
$uid = $_GET['id'];
$userdataSql = "select * from `user` WHERE id = {$uid}";
$userdata = $mysql->query($userdataSql);
$userdata = $userdata[0];
if ($_POST['count']) {
    $count = $_POST['count'];
	if(!preg_match("/^\d*$/",$count)){
		echo "<script>alert('请输入1000的倍数！')</script>";
        echo "<script>location.href('./gold.php?id={$uid}&jtime={$openid}')</script>";
	}else if ($count % 1000 != 0) {
        echo "<script>alert('请输入1000的倍数！')</script>";
        echo "<script>location.href('./gold.php?id={$uid}&jtime={$openid}')</script>";
    }else if($userdata['total']<$count){
    	echo "<script>alert('积分不足！')</script>";
        echo "<script>location.href('./gold.php?id={$uid}&jtime={$openid}')</script>";
    }else {
        $gold = $userdata['gold'] + $count / 1000;
        $total = $userdata['total'] - $count;
        //echo $total."<br>".$gold;exit;
        //往score_detail里写入数据，增加金币收入
        $scoreArr = array(
            'uid' => $uid,
            'content' => '积分兑换金币',
            'score' => $count / 1000,
            'type' => 1,
            'day' => date("Y-m-d H:m:s", time())
        );
        $goldSql = "insert into `score_detail`" . arr2s($scoreArr);
        $mysql->execute($goldSql);
        //往send里写数据，记录积分支出
        $scoreArr = array(
            'uid' => $uid,
            'content' => '积分兑换金币',
            'day' => date("Y-m-d H:m:s", time()),
            'score' => $count,
            'type' => 0
        );
        $scoreSql = "insert into `send`" . arr2s($scoreArr);
        $mysql->execute($scoreSql);
        //修改user表数据
        $updateSql = "update `user` set `gold` = {$gold},`total` = {$total} WHERE `id` = {$uid}";
        if ($mysql->execute($updateSql)) {
            echo "<script>alert('兑换成功！')</script>";
            echo "<script>location.href='./gold.php?id={$uid}&jtime={$openid}'</script>";
        }
        echo "<script>alert('兑换失败！')</script>";
    }
}
?>
<html>
<head>
    <meta charset="UTF-8">
    <title>微有趣</title>
    <meta name="keywords" content=""/>
    <meta name="description" content=""/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
    <link rel="stylesheet" type="text/css" href="./assets/css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="./assets/css/gold.css"/>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
        function changeSize() {
            var propo = $(window).width() / 640;
            $('html').css('font-size', 40 * propo + 'px');
        }
        ;
        changeSize();
        $(window).resize(function () {
            changeSize();
        });
    </script>
</head>
<body>
<div class="wrap">
    <div id="s" class="s_tan" style="display: none;">
        <div class="inp">
            <form method="post">
                <h3>积分兑换金币</h3>
                <input name="count" id="s_ipt" type="text" placeholder="请输入需要兑换的积分数"/>

                <p>输入的积分数应为1000的倍数</p>
                <button type="submit">确定</button>
                <button id="s_cancel" type="reset">取消</button>
            </form>
        </div>
    </div>
    <div class="header">
        积分兑换金币比率：1000积分=1金币=1元
    </div>
    <div class="con">
        <div class="change">
            <div class="money clearfix">
                <span class="icon1"></span><span class="money_num"><?= $userdata['gold'] ?>元</span>
                <a href="./focus.php?id=<?php echo $uid ?>&jtime=<?php echo $openid?>" class="money_btn">金币提现</a>
            </div>
            <div class="score clearfix">
                <span class="icon2"></span><span class="score_num"><?= $userdata['total'] ?></span><a class="score_btn">兑换金币</a>
            </div>
        </div>
        <div class="more">
            <ul class="list">
                <li>
                    <div class="left">
                        <h3>1)每日阅读</h3>

                        <p>选择自己喜欢的文章认真阅读即可获得积分</p>
                    </div>
                    <a onclick="jumpArticles()" class="btn">阅读文章</a>
                </li>
                <li>
                    <div class="left">
                        <h3>2)发表文章</h3>

                        <p>展示自己的文采，发表自己的文章即可获得积分</p>
                    </div>
                    <a onclick="jumpPublish()" class="btn">发表文章</a>
                </li>
                <li>
                    <div class="left">
                        <h3>3)发表评论</h3>

                        <p>对发表的文章进行评论，即可获得相应的积分</p>
                    </div>
                    <a onclick="jumpNews()" class="btn">立即评论</a>
                </li>
                <li class="clearfix">
                    <div class="left">
                        <h3>4)更多获取积分方式</h3>
                    </div>
                    <a onclick="jumpPage();" class="btn">查看更多</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<script type="text/javascript">
    //积分兑换金币
    $(".score_btn").click(function () {
        $("#s").show();
    });
    $("#s_cancel").click(function () {
        $("#s").hide();
    });
    $("#s_ipt").focus(function () {
        $(".s_tan .inp").css("margin-top", "3rem");
    });
    $("#s_ipt").blur(function () {
        $(".s_tan .inp").css("margin-top", "7rem");
    });
    function jumpNews(){
				window.jsObj.readyGoNews();//首页
			}
	function jumpUser(){
				window.jsObj.readyGoUser();//微信绑定
			}
	function jumpPublish(){
				window.jsObj.readyGoPublish();//发表
			}
	function jumpArticles(){
				window.jsObj.readyGoArticles();//文章
			}
	function jumpPage(){
				window.location.href = "http://<?=$row_user[0]['link']?>/weiApp/task.php";//页面
			}
</script>
</body>
</html>
